package com.yao.club.controller.goods;

import com.netease.yidun.sdk.core.utils.StringUtils;
import com.yao.club.entity.dto.GoodsCollectRecord;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.service.GoodsCollectService;
import com.yao.club.token.PassToken;
import com.yao.club.token.UserToken;
import com.yao.club.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Api(tags = "俱乐部商城-商品收藏")
@RestController
@RequestMapping("/mall/goodsCollect")
public class GoodsCollectController {

    @Autowired
    private GoodsCollectService goodsCollectService;
    @Resource
    HttpServletRequest request;


    @UserToken
    @ApiOperation(value = "收藏/取消收藏商品")
    @PostMapping("/toggle/{goodsId}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<Boolean> toggleCollect(
            @PathVariable Long goodsId) {
        String username = (String) request.getAttribute("username");
        return goodsCollectService.toggleCollect(goodsId, username);
    }

    @UserToken
    @ApiOperation(value = "检查商品收藏状态")
    @GetMapping("/check/{goodsId}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Long", paramType = "path")
    })
    public ResponseEntity<Boolean> checkCollectStatus(
            @PathVariable Long goodsId) {
        String username = (String) request.getAttribute("username");
        return goodsCollectService.checkCollectStatus(goodsId, username);
    }

    @UserToken
    @ApiOperation(value = "查看自己收藏的商品")
    @GetMapping("/view")
    public ResponseEntity<ResultEntity<GoodsCollectRecord>> viewCollectedGoods() {
        String username = (String) request.getAttribute("username");
        return goodsCollectService.getCollectedGoods(username);
    }
} 